package com.rongzhe.house.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.manager.OrderManager;
import com.rongzhe.house.presenter.PayTypePresenter;
import com.rongzhe.house.ui.view.NoScrollListview;
import com.rongzhe.house.utils.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by huang on 2018/1/4.
 */

public class PayTypeActivity extends BaseActivity<PayTypePresenter> {
    @BindView(R.id.list_pay_type)
    NoScrollListview listPayType;
    @BindView(R.id.item_alipay)
    RadioButton itemAlipay;
    @BindView(R.id.item_weixinpay)
    RadioButton itemWeixinpay;
    private PayTypeAdapter mPayTypeAdapter;

    @Override
    protected PayTypePresenter createPresenter() {
        return new PayTypePresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_type);
        ButterKnife.bind(this);
        mPayTypeAdapter = new PayTypeAdapter();
        listPayType.setAdapter(mPayTypeAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected String getActionTitle() {
        return getString(R.string.choose_pay_type);
    }

    private class PayTypeAdapter extends BaseAdapter {
        private List<Dictionary> data;

        public void setPayTypeData(List<Dictionary> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public Object getItem(int position) {
            return data == null ? null : data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) getLayoutInflater()
                    .inflate(R.layout.list_item_text, parent, false);
            Dictionary item = data.get(position);
            textView.setTag(item);
            textView.setText(item.getName());
            return textView;
        }
    };

    @OnClick({R.id.item_alipay, R.id.item_weixinpay})
    public void onPayWayClick(View view) {
        int id = view.getId();
        if (id == R.id.item_alipay) {
            OrderManager.getInstance().setPayWay(OrderManager.PayWay.ALI);
        } else {
            OrderManager.getInstance().setPayWay(OrderManager.PayWay.WEIXIN);
        }
    }

    @OnItemClick(R.id.list_pay_type)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (OrderManager.getInstance().getPayWay() == null) {
            showAlert(UIUtils.createHintDialog(PayTypeActivity.this, getString(R.string.hint),
                    getString(R.string.need_choose_pay_way), null));
            return;
        }
        Dictionary dictionary = (Dictionary) view.getTag();
        mPresenter.onPayTypeChoosed(dictionary.getCode());
    }

    public void setPayTypeData(List<Dictionary> data) {
        mPayTypeAdapter.setPayTypeData(data);
    }
}
